define(function () {
    'use strict';

    function ctrl($scope,$q,InfoService,HistoryNavChartService,$ionicLoading,$filter,$stateParams, CommonService) {

        $scope.channel = {};
        $scope.vm = {};
        $scope.channel.channelId = $stateParams['id'];
        $scope.channel.title = "LPAUGBPOZ.IDC";
        $scope.goldChartConfig = HistoryNavChartService.historyIndexChart(null);

        init();

        $scope.doRefresh = function() {
            init().finally(function(){
                $scope.$broadcast('scroll.refreshComplete');
            });
        };

        $scope.loadMore = function() {
            return queryContentBriefs();
        };

        function init(){
            if(!$scope.funds){
                $ionicLoading.show();
            }
            $scope.funds = new Array();
            $scope.page_no = 1;
            $scope.vm.loadMore = true;
            return $q.all([queryIndexProfit(), queryHistoryIndex()]);
        }


        //查询指数收益率
        function queryIndexProfit(){
            var p = InfoService.getIndexProfit($scope.channel.title);
            p.then(function(data){
                if(data){
                    $scope.gold = data[0];
                }
            });
            return p;
        }

        //绘制走势图
        function queryHistoryIndex(){
            var p = InfoService.getHistoryIndex($scope.channel.title, 60);
            p.then(function(data){
                if(data && data.length > 0){
                    //画收盘走势图
                    $scope.goldChartConfig = HistoryNavChartService.historyIndexChart(data);
                    $scope.data_date = CommonService.formatDate(data[data.length - 1].data_date);
                }
            });
            return p;
        }

        //查询基金概况
        function queryContentBriefs(){
            var p = InfoService.getProductsByChannelId($scope.channel.channelId, $scope.page_no);
            p.then(function(data){
                if(data){
                    if(data && data.length > 0){
                        for(var i=0;i<data.length;i++){
                            var fund = data[i];
                            fund.prod_min_subscribe = $filter('prodMinSubscribe')(CommonService.getMinAmount(fund));
                        }
                        $scope.funds = $scope.funds.concat(data);
                        $scope.page_no++;
                    } else {
                        $scope.vm.loadMore = true;
                    }
                    $scope.$broadcast('scroll.infiniteScrollComplete');
                }
            });
            $ionicLoading.hide();
            return p;
        }

    }

    ctrl.$inject = ['$scope','$q','InfoService','HistoryNavChartService','$ionicLoading','$filter', '$stateParams', 'CommonService'];
    return ctrl;
});